package cn.zjyj.module.zjyj.service.riskapplyrecord;

import cn.hutool.core.collection.CollUtil;
import cn.zjyj.module.zjyj.dal.dataobject.riskidentify.RiskIdentifyDO;
import cn.zjyj.module.zjyj.dal.mysql.deviceinstance.DeviceInstanceMapper;
import cn.zjyj.module.zjyj.dal.mysql.riskassessmentdetail.RiskAssessmentDetailMapper;
import cn.zjyj.module.zjyj.dal.mysql.riskassessmentinstancerel.RiskAssessmentInstanceRelMapper;
import cn.zjyj.module.zjyj.dal.mysql.riskidentify.RiskIdentifyMapper;
import cn.zjyj.module.zjyj.service.risklist.RiskListService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.util.stream.Collectors;

import cn.zjyj.module.zjyj.controller.admin.riskapplyrecord.vo.*;
import cn.zjyj.module.zjyj.dal.dataobject.riskapplyrecord.RiskAssessmentApplyRecordDO;
import cn.zjyj.framework.common.common.pojo.PageResult;
import cn.zjyj.framework.common.common.util.object.BeanUtils;

import cn.zjyj.module.zjyj.dal.mysql.riskapplyrecord.RiskAssessmentApplyRecordMapper;

import static cn.zjyj.framework.common.common.exception.util.ServiceExceptionUtil.exception;
import static cn.zjyj.framework.common.common.util.collection.CollectionUtils.convertList;
import static cn.zjyj.module.zjyj.enums.ErrorCodeConstants.*;

/**
 * 风险申请记录 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class RiskAssessmentApplyRecordServiceImpl implements RiskAssessmentApplyRecordService {

    @Resource
    private RiskAssessmentApplyRecordMapper riskAssessmentApplyRecordMapper;

    @Resource
    RiskIdentifyMapper riskIdentifyMapper;

    @Resource
    RiskListService riskListService;

    @Resource
    RiskAssessmentDetailMapper riskAssessmentDetailMapper;

    @Resource
    RiskAssessmentInstanceRelMapper riskAssessmentInstanceRelMapper;

    @Resource
    DeviceInstanceMapper deviceInstanceMapper;

    @Override
    public Long createRiskApplyRecord(RiskAssessmentApplyRecordSaveReqVO createReqVO) {
        // 插入
        RiskAssessmentApplyRecordDO riskApplyRecord = BeanUtils.toBean(createReqVO, RiskAssessmentApplyRecordDO.class);
        riskAssessmentApplyRecordMapper.insert(riskApplyRecord);
        // 返回
        return riskApplyRecord.getId();
    }

    @Override
    public void updateRiskApplyRecord(RiskAssessmentApplyRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateRiskApplyRecordExists(updateReqVO.getId());
        // 更新
        RiskAssessmentApplyRecordDO updateObj = BeanUtils.toBean(updateReqVO, RiskAssessmentApplyRecordDO.class);
        riskAssessmentApplyRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteRiskApplyRecord(Long id) {
        // 校验存在
        validateRiskApplyRecordExists(id);
        // 删除
        riskAssessmentApplyRecordMapper.deleteById(id);
    }

    @Override
    public void deleteRiskApplyRecordListByIds(List<Long> ids) {
        // 校验存在
        validateRiskApplyRecordExists(ids);
        // 删除
        riskAssessmentApplyRecordMapper.deleteByIds(ids);
    }

    private void validateRiskApplyRecordExists(List<Long> ids) {
        List<RiskAssessmentApplyRecordDO> list = riskAssessmentApplyRecordMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(RISK_APPLY_RECORD_NOT_EXISTS);
        }
    }

    private void validateRiskApplyRecordExists(Long id) {
        if (riskAssessmentApplyRecordMapper.selectById(id) == null) {
            throw exception(RISK_APPLY_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public RiskAssessmentApplyRecordDO getRiskApplyRecord(Long id) {
        return riskAssessmentApplyRecordMapper.selectById(id);
    }

    @Override
    public PageResult<RiskAssessmentApplyRecordRespVO> getRiskApplyRecordPage(RiskAssessmentApplyRecordPageReqVO pageReqVO) {
        PageResult<RiskAssessmentApplyRecordDO> riskApplyRecordDOPageResult = riskAssessmentApplyRecordMapper.selectPage(pageReqVO);
        List<RiskAssessmentApplyRecordDO> list = riskApplyRecordDOPageResult.getList();
        if (CollUtil.isEmpty(list)) {
            return new PageResult<>(Collections.emptyList(), 0L);
        }
        PageResult<RiskAssessmentApplyRecordRespVO> pageResult = BeanUtils.toBean(riskApplyRecordDOPageResult, RiskAssessmentApplyRecordRespVO.class);

        List<Long> collect = list.stream().map(RiskAssessmentApplyRecordDO::getIdentifyId).collect(Collectors.toList());

        List<RiskIdentifyDO> riskIdentifyDOS = riskIdentifyMapper.selectByIds(collect);

        pageResult.getList().stream().forEach(item -> riskIdentifyDOS.stream().filter(riskIdentify -> item.getIdentifyId() == riskIdentify.getId()).findFirst().ifPresent(riskIdentify -> {
            item.setRiskType(riskIdentify.getRiskType());
            Long riskTypeInfoId = riskIdentify.getRiskTypeInfoId();
            String riskName = riskListService.getRiskName(riskIdentify.getRiskType(), riskTypeInfoId);
            item.setRiskInfoName(riskName);
        }));
        return pageResult;
    }


//    @Override
//    public void save(Long applyRecordId, RiskIdentifyDO identify, RiskIdentifyObjectDO objectDO, RiskIdentifyObjectContentDO contentDO) {
//        RiskAssessmentDetailDO detailDO = RiskAssessmentDetailDO.builder()
//                .applyRecordId(applyRecordId)
//                .identifyId(identify.getId())
//                .riskType(identify.getRiskType())
//                .identifyObjectId(objectDO.getId())
//                .identifyObjectName(objectDO.getRiskTarget())
//                .identifyObjectContentId(contentDO.getId())
//                .identifyContentCode(contentDO.getIdentifyContentCode())
//                .identifyContent(contentDO.getIdentifyContent())
//                .hazard(contentDO.getHazard())
//                .projectSolution(contentDO.getProjectSolution())
//                .managerSolution(contentDO.getManagerSolution())
//                .selfSolution(contentDO.getSelfSolution())
//                .trainEduSolution(contentDO.getTrainEduSolution())
//                .badSolution(contentDO.getBadSolution()).build();
//
//        List<DeviceInstanceDO> instances = deviceInstanceMapper.selectList(DeviceInstanceDO::getDeviceId, identify.getRiskTypeInfoId());
//        if (CollUtil.isEmpty(instances)) {
//            return;
//        }
//        detailDO.setInstanceSize(instances.size());
//        riskAssessmentDetailMapper.insert(detailDO);
//
//        instances.stream().forEach(item -> {
//            RiskAssessmentInstanceRelDO rel = RiskAssessmentInstanceRelDO.builder()
//                    .applyRecordId(applyRecordId)
//                    .assessmentDetailId(detailDO.getId())
//                    .instanceId(item.getId())
//                    .identifyContentCode(contentDO.getIdentifyContentCode() + "_" + item.getCode()).build();
//
//            riskAssessmentInstanceRelMapper.insert(rel);
//        });
//
//
//    }
}